$(() => {
	var List =[];
  // 生成二维码
  if (localStorage.getItem("promotionCode")) {
    $("#qr").qrcode(
      "http://www.hanyuan365.com/show/games?promotionCode=" +
        localStorage.getItem("promotionCode")
    );
  } else {
    $("#qr").qrcode("http://www.hanyuan365.com/show/games/");
  }

  // 页面加载完成 检测localstorage是否存在已知用户信息
  (() => {
    var loginData = {
      loginName: localStorage.getItem("loginName"),
      passWord: localStorage.getItem("passWord")
    };
    $.post('http://121.43.172.175/api/pclogin.html', loginData, (data) => {
    // $.post("http://192.168.0.125:8081/api/pclogin.html", loginData, data => {
		console.log(data);
		
      if (data.status == 1) {
        $(".btngroup").hide();
        $(".logined").show();
        $(".closebtn").show();
        $(".gameusername").html(data.data.fNickName);
        $("#ptotal").html(data.data.ptotal);
		$("#tuijian").html(data.data.promotionCode);
    $('#dqtl').html(data.data.foodCount);
    $('#renshu').html(data.data.tgList.length);
    List = data.data.tgList;
    $('#tgrs').html(List.length);
    
		for (var i = 0; i < List.length; i++) {
			appendDom(
			  time(List[i].fRegisterTime),
			  mdphone(List[i].fTelephone),
			  List[i].fNickName
			);
		  }
      } else {
        // $('.log1,.opcy').show();
      }
    });
  })();

  // 手机号加密处理
  function mdphone(phone) {
    var phoneCondensation = phone.substr(0, 3) + "****" + phone.substr(7);
    return phoneCondensation;
  }

  // 页面加载成功加载推广记录
  function appendDom(time, phone, nickname) {
    // var li ="<li><span>" +time +"</span><span>" + phone +"</span><span>" +nickname +"</span></li>";
    var domdiv = '<div class="nrow"><div class="nname">'+nickname+'</div><div class="nphone">'+phone+'</div><div class="ndate">'+time+'</div></div>';
    $(".yaoqing").append(domdiv);
  }

  // 清理form表单
  function clearForm() {
    $("#long_name_input").val("");
    $("#loginname").val("");
    $("#long_password_input").val("");
    $("#passsword").val("");
    $("#forphone").val("");
    $("#forphoneinfo").val("");
    $("#formsg").val("");
    $("#fortext").val("");
    $("#newpassword").val("");
    $("#newinfo").val("");
    $("#confirmnewpassword").val("");
    $("#confirmnewinfo").val("");
    $("#nick").val("");
    $("#nickname").val("");
    $("#phone").val("");
    $("#waringphone").val("");
    $("#msgcode").val("");
    $("#smstext").val("");
    $("#loginpw").val("");
    $("#regpassword").val("");
    $("#loginpw2").val("");
    $("#regconfirmpwd").val("");
    $("#dealpw").val("");
    $("#paypassword").val("");
    $("#dealpw2").val("");
    $("#confirmpaypwd").val("");
  }
  // 关闭弹框并清理form表单
  $(".dialog .close").click(function() {
    clearForm();
    $(".dialog,.opcy").hide();
  });

  // 发送验证码
  function sendsms(phoneNumber, callback) {
    $.post(
      "http://121.43.172.175/api/regcodes.html", {
        phone: phoneNumber
      },
      data => {
        callback(data);
      }
    );
  }

  // 昵称输入框获取焦点提示消失
  $("#phone").focus(() => {
    hiddeninfo("waringphone");
  });
  // 手机输入框获取焦点提示消失
  $("#nickname").focus(() => {
    hiddeninfo("nick");
  });
  // 验证码输入框获取焦点提示消失
  $("#msgcode").focus(() => {
    hiddeninfo("smstext");
  });
  // 登录输入框获取焦点提示消失
  $("#loginpw").focus(() => {
    hiddeninfo("regpassword");
  });
  // 确认登录输入框获取焦点提示消失
  $("#loginpw2").focus(() => {
    hiddeninfo("regconfirmpwd");
  });
  // 支付输入框获取焦点提示消失
  $("#dealpw").focus(() => {
    hiddeninfo("paypassword");
  });
  // 确认支付输入框获取焦点提示消失
  $("#dealpw2").focus(() => {
    hiddeninfo("confirmpaypwd");
  });

  // 获取扫码进入之后的promotionCode;
  var Request = new Object();
  Request = GetRequest();
  var pCode = Request["promotionCode"];

  var promotionCode = pCode;
  if (promotionCode) {
    // $("#popcode").focus();
    $("#popcode").val(promotionCode);
  }

  //发送验证码按钮
  $("#sendbutn").click(() => {
    var phoneNumber = $("#phone").val();
    if (phoneNumber && isPoneAvailable(phoneNumber)) {
      // 验证码倒计时
      $("#sendbutn").html("60s");
      var counter = 59;
      const smsgtimer = setInterval(function() {
        if (counter == 0) {
          $("#sendbutn").html("发送验证码");
          clearInterval(smsgtimer);
        } else {
          $("#sendbutn").html(counter + "s");
          counter--;
        }
      }, 1000);
      sendsms(phoneNumber, data => {
        if (data.status == 0) {
          waringinfo("smstext", "msgcode", data.msg);
        }
      });
    } else {
      $("#phone").val("");
      waringinfo("waringphone", "phone", "请输入有效手机号");
    }
  });

  // 点击下一步按钮 并验证是否有空
  $("button.step1").click(function() {
    const fistdata = {
      nickName: $("#nickname").val(),
      phone: $("#phone").val(),
      valiCode: $("#msgcode").val()
    };
    if (fistdata.nickName == "") {
      waringinfo("nick", "nickname", "请输入昵称");
      return;
    }
    if (fistdata.nickName.length > 5) {
      waringinfo("nick", "nickname", "昵称大小不能超过5");
      return;
    }
    if (fistdata.phone == "") {
      waringinfo("waringphone", "phone", "请输入手机号");
      return;
    }
    if (fistdata.valiCode == "") {
      waringinfo("smstext", "msgcode", "请输入验证码");
      return;
    }
    $(".step1").hide();
    $(".step2").show();
  });

  // 点击注册按钮
  $("#goregistered").click(function() {
    const registereddata = {
      fnickName: $("#nickname").val(),
      floginName: $("#phone").val(),
      valiCode: $("#msgcode").val(),
      floginPassword: $("#loginpw").val(),
      fTradePassword: $("#dealpw").val(),
      promotionCode: $("#popcode").val()
    };

    if (registereddata.floginPassword == "") {
      waringinfo("regpassword", "loginpw", "请输入登录密码");
      return;
    }
    if (registereddata.floginPassword != $("#loginpw2").val()) {
      waringinfo("regconfirmpwd", "loginpw2", "两次输入的登录密码不相同");
      return;
    }
    if (registereddata.fTradePassword == "") {
      waringinfo("paypassword", "dealpw", "请输入交易密码");
      return;
    }
    if (registereddata.fTradePassword != $("#dealpw2").val()) {
      waringinfo("confirmpaypwd", "dealpw2", "两次输入的交易密码不相同");
      return;
    }
    console.log(registereddata);

    $.post("http://121.43.172.175/api/register.html", registereddata, data => {
        console.log(data);

        if (data.status == 1) {
          clearForm();
          $(".dialog,.opcy").hide();
        } else {
        }
      }
    );
  });

  // 点击输入框取消警告提示
  $("#long_name_input").focus(() => {
    hiddeninfo("loginname");
    hiddeninfo("loginwaringinfo");
  });
  $("#long_password_input").focus(() => {
    hiddeninfo("loginwaringinfo");
  });
  $("#long_password_input").focus(() => {
    hiddeninfo("passsword");
  });



  // 登录按钮
  $("#loginbutton").click(function() {
    // 获取帐号和密码
    var logindata = {
      loginName: $("#long_name_input").val(),
      passWord: $("#long_password_input").val()
    };
    // 判断是否有空
    if (logindata.loginName == "") {
      waringinfo("loginname", "long_name_input", "请输入用户名");
      return;
    }
    if (logindata.passWord == "") {
      waringinfo("passsword", "long_password_input", "请输入密码");
      return;
    }

    // 执行登录请求
    $.post("http://121.43.172.175/api/pclogin.html", logindata, data => {
    // $.post("http://192.168.0.125:8081/api/pclogin.html", logindata, data => {
      console.log(data);
      if (data.status == 1) {
        clearForm();
        // location.reload();
		$("#ptotal").html(data.data.ptotal);
		$('#dqtl').html(data.data.foodCount);
        $("#tuijian").html(data.data.promotionCode);
        clearForm();
        $(".dialog,.opcy").hide();
        $(".btngroup").hide();
        $(".logined").show();
        let localdata = {
          loginName: logindata.loginName,
          passWord: logindata.passWord,
          fid: data.data.fid,
          promotionCode: data.data.promotionCode
        };
        let locals = ["loginName", "passWord", "fid", "promotionCode"];
        locals.forEach(v => {
          localStorage[v] = localdata[v];
        });
        $(".closebtn").show();
    $(".gameusername").html(data.data.fNickName);
    $('#renshu').html(data.data.tgList.length);
		List = data.data.tgList;
		for (var i = 0; i < List.length; i++) {
			appendDom(
			  time(List[i].fRegisterTime),
			  mdphone(List[i].fTelephone),
			  List[i].fNickName
			);
		  }
      } else {
        waringinfo("loginwaringinfo", "", data.msg);
      }
    });
  });



  // 登出操作
  $("#loginout").click(() => {
    // localStorage.removeItem('fid');
    // localStorage.removeItem('loginName');
    // localStorage.removeItem('passWord');
    localStorage.clear();
    location.reload();
    $(".btngroup").show();
    $(".closebtn").hide();
    $(".logined").hide();
  });

  // 忘记密码
  $("#forgetbutton").click(function() {
    $(".log1").hide();
    $(".log2").show();
  });

  // 忘记密码手机区域点击去除警告提示
  $("#forphone").focus(() => {
    hiddeninfo("forphoneinfo");
  });
  $("#formsg").focus(() => {
    hiddeninfo("fortext");
  });
  $("#newpassword").focus(() => {
    hiddeninfo("newinfo");
  });
  $("#confirmnewpassword").focus(() => {
    hiddeninfo("confirmnewinfo");
  });
  //忘记密码发送验证码
  $("#sendformsg").click(function() {
    let forphone = $("#forphone").val();
    if (forphone && isPoneAvailable(forphone)) {
      // 验证码倒计时
      $("#sendformsg").html("60s");
      var counter = 59;
      const smsgtimer = setInterval(function() {
        if (counter == 0) {
          $("#sendformsg").html("发送验证码");
          clearInterval(smsgtimer);
        } else {
          $("#sendformsg").html(counter + "s");
          counter--;
        }
      }, 1000);
      $.post(
        "http://121.43.172.175/api/forget/first.html",
        {
          floginName: forphone
        },
        data => {
          if (data.status == 0) {
            waringinfo("fortext", data.msg);
          } else {
          }
        }
      );
    } else {
      waringinfo("forphoneinfo", "请输入手机号码");
    }
  });

  // 提交修改密码
  $("#forgotgo").click(function() {
    let forgotdata = {
      floginName: $("#forphone").val(),
      valiCode: $("#formsg").val(),
      newPassword: $("#newpassword").val(),
      newPasswords: $("#confirmnewpassword").val()
    };
    //验空
    if (forgotdata.floginName == "") {
      waringinfo("forphoneinfo", "请输入手机号码");
      return;
    }
    if (forgotdata.valiCode == "") {
      waringinfo("fortext", "请输入验证码");
      return;
    }
    if (forgotdata.newPassword == "") {
      waringinfo("newinfo", "请输入新密码");
      return;
    }
    if (forgotdata.newPassword != forgotdata.newPasswords) {
      waringinfo("confirmnewinfo", "两次输入的新密码不一致");
      return;
    }
    $.post("http://121.43.172.175/api/forget/last.html", forgotdata, data => {
      if (data.status == 1) {
        clearForm();
        $(".dialog,.opcy").hide();
      } else {
        waringinfo("confirmnewinfo", "两次输入的新密码不一致");
      }
    });
  });
  // 推广历史记录
  $("#forgetbuttom").click(function() {
    $(".pop1").hide();
    $(".pop2").show();
  });

  $("#tuijian").click(() => {
    $(".pop1").show();
  });

  // 跳转去商城
  $("#goshop").click(() => {
    var userInfo = {
      fid: localStorage.getItem("fid"),
      loginName: localStorage.getItem("loginName"),
      passWord: localStorage.getItem("passWord")
    };
    if (userInfo.fid && userInfo.loginName && userInfo.passWord) {
      window.location.href = "shouji.html";
    } else {
      $(".log1").show();
    }
  });

  // 推荐有奖
  $("#tuijian").click(() => {
    console.log("1");

    // $('.dialog,.popJB,.pop1').show()
  });
});
